
import { reqgetcate,reqgetgoods } from "../../request/api"
const initState={
    catelist:[],
    categoods:[],
    n:1
}

const types={
    CAHNEGE_CATELISET:'CATE_CHANGE_CATELIST',
    CHANGE_CATEGOODS:'CATE_CHANGECATEGOODS',
    CHANGE_N:'CATE_CHANGEN'
}

const reducer =(state=initState,action)=>{
        switch(action.type){
            case types.CAHNEGE_CATELISET:
                return {
                    ...state,
                    catelist:action.arr
                }
            case types.CHANGE_CATEGOODS:
                return {
                    ...state,
                    categoods:action.arr       
                }
            case types.CHANGE_N:
                return {
                    ...state,
                    n:action.n
                }
            default:
                return state
        }
}   

export const actions={
    changecatelist:(arr)=>({type:types.CAHNEGE_CATELISET,arr}),
    changecategoods:(arr)=>({type:types.CHANGE_CATEGOODS,arr}),
    changen:n=>{
        return {type:types.CHANGE_N,n}
    },

    reqchangelist:()=>(dispatch, getState)=>{
        let { catelist } = getState().cate
        if (catelist.length > 0) return;
        reqgetcate().then(res=>{
            dispatch(actions.changecatelist(res.data.list))
        })
    },
    reqcategoods:(n)=>(dispatch,getState)=>{
        // {fid:n}
        dispatch(actions.changen(n))
        
        reqgetgoods({fid:n}).then(res=>{
            dispatch(actions.changecategoods(res.data.list))
        })
    }
}

//导出数据
export const getcatelist=state=>state.cate.catelist

export const getcategoods=state=>state.cate.categoods

export const getn=state=>state.cate.n

export default reducer